<?php 
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller{
	public function yzm(){
		ob_clean();
		$verify = new \Think\Verify();
		$verify->imageW = 135;
		$verify->imageH = 40;
		$verify->fontSize =20;
		$verify->length = 4;
		$verify->useNoise = false;
		$verify->entry();
	}

	public function checkYzm(){
		$verify = new \Think\Verify();
		if($verify->check($yzm)){
			return true;
		}else{
			exit('验证码错误');
		}
	}

	public function login(){
		if(!IS_POST){
			$this->display();
		}else{
			if($this->checkYzm(I('post.yzm'))){
				$user = D('user')->where('username='.'"'.I('post.password').'"')->find();
				if(!md5(I('post.password').$user['salt'=]) === $user['password']){
					echo '密码或用户名错误';
				}else{
					cookie('username',$user['username']);
					cookie('coode',md5($user['username'].C('salt')));
					$this->redirect('/')
				}
			}else{
				echo '验证码错误';
			}
		}
	}

	public function logout(){
		cookie('username',null);
		cookie('coode',null);
		$this->redirect('/');
	}

	public function msg(){
		$this->display();
	}

	public function reg(){
		if(!IS_POST){
			$this->display();
		}else{
			$user->D('User');
			if(!$user->create()){
				echo $user->getError();
			}else{
				$yan = $this->yan();
				$user->password = md5($user->password.$yan);
				$user->yan();
				if($user->add()){
					$this->redirect('Home/User/login');
				}
			}
		}
	}

	public function yan(){
		return mt_rand(10000,99999);
	}
}